namespace CountPrimes
{
    public class Solution
    {
        public string CountAndSay(int n)
        {
            var result = "1";
            if (n == 1)
                return result;

            for (var k = 2; k <= n; k++)
            {
                var temp = string.Empty;
                for (var i = 0; i < result.Length;)
                {
                    int count = 1, j;
                    for (j = i + 1; j < result.Length; j++)
                        if (result[i] == result[j])
                            count++;
                        else
                            break;

                    temp += $"{count}{result[i]}";
                    i = j;
                }

                result = temp;
            }

            return result;
        }
    }
}